console.log('保存新增: => ', $event);
const $form = this.getApiPage().getApiWidgetByAliasName('change-form')?.widgetObj;
const $list = this.getApiPage().getApiWidgetByAliasName('新增演练人员')?.widgetObj;
const gis = this.getApiPage().getApiWidgetByAliasName('三维组件')?.widgetObj;
const edits = this.getApiPage().getApiWidgetsByClassName('edit');
const projects = this.getApiPage().getDataById('848159656336424960')?.data || [];
const levels = this.getApiPage().getDataById('744574925553008640')?.data || [];
const types = this.getApiPage().getDataById('744575296061046784')?.data || [];
const eventNatures = this.getApiPage().getDataById('756194853578080256')?.data || [];
const sourceSystem = this.getApiPage().getDataById('800764940934709248')?.data || [];
const prefix = this.pageObj.getVariables().find(v => v.name === "environmentType")?.value;
// 路由参数
let params      = this.pageObj.getVariables().find(v => v.name === "screen_params")?.value;
const apiPage = this.getApiPage();


const requestDrill = (url, body) => {
    const options = {
        method: 'POST',
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(body)
    };
    return fetch(url, options).then(res => res.json()).then(res => {
        // 跳转到启动演练
        return res
    })
};


function updateDrill(dataSource) {
    requestDrill(`/${prefix}Event/drillPlanInfo/update`, dataSource).then(({ success }) => {
        if (success) {
            $list?.hide();
            layer?.msg('变更演练成功!');
            console.log("变更演练成功_123，跳转到启动方案页面！");

            edits?.forEach(v => v.hideWidget());
            gis?.setIconPosition({ right: 20, top: 100 });
            gis?.setModalPosition({ right: 405, bottom: 20, maxWidth: 1510, width: 810, height: 286, maxHeight: 960 });
            gis?.gisMap.showOrHideAllArea(false);
            apiPage.setVariables([{ name: 'update', value: dataSource.id }]);
            // 跳转到启动方案页面！
            window.parent.postMessage({ type: 'nav/style', pageName: '演练' }, document.referrer);
            apiPage.navigateStage(
                '843567423519981568',
                [
                    { name: 'screen_params', value: JSON.stringify({ ...params, ...dataSource}) },
                ]
            );
        }
    });
}

if ($form) {
    layui.form.submit($form._option.instanceName, (form) => {
        let dataSource = Object.entries(form.field).reduce((def, [key, value]) => value !== '' ? (def[key] = value, def) : def, {});
        const type = types.find(v => v.value === dataSource.eventTypeCode);
        const level = levels.find(v => v.value === dataSource.eventGradeCode);
        const quality = eventNatures.find(v => v.value === dataSource.eventQualityCode);
        const project = projects?.find(v => v.value === dataSource.projectCode);
        let [ startTime, endTime ] = dataSource.drillDatetimeRange?.split(' - ') || [];

        if (!endTime) {
            [ startTime, endTime ] = dataSource.drillDatetimeRange?.split(' 至 ')
        }

        try {
            if (params) {
                params = JSON.parse(params)
                // 演练名称
                if (dataSource.drillName?.trim().length > 50) return layer?.msg('演练名称请保持50字以内');
                // 演练范围
                if (dataSource.eventRange?.trim().length > 500) return layer?.msg('演练范围请保持500字以内');
                // 周边环境
                if (dataSource.eventEnvironment?.trim().length > 500) return layer?.msg('周边环境请保持500字以内');
                // 演练目的
                if (dataSource.drillSake?.trim().length > 500) return layer?.msg('演练目的请保持500字以内');
                // 演练内容
                if (dataSource.drillContent?.trim().length > 5000) return layer?.msg('演练内容请保持5000字以内');

                const body = {
                    // ...params,
                    id: params.id,
                    // 演练来源
                    projectName: project.name                       || params.projectName,
                    projectCode: project.value                      || params.projectCode,
                    projectId: project.id                           || params.projectId,
                    // 演练状态(-2-修改；0-未演练；1-已演练；2-演练中；4-变更)
                    disableFlag: 4,
                    // 演练内容
                    drillContent: dataSource.drillContent           || params.drillContent,
                    // 演练区域
                    drillLocation: dataSource?.drillLocation        || params.drillLocation,
                    // 演练区域id
                    drillLocationId: dataSource?.drillLocationId    || params.drillLocationId,
                    // 演练名称
                    drillName: dataSource.drillName                 || params.drillName,
                    // 演练组织典信息
                    // drillPersonInfoDictTreeNodes: [],
                    // 演练目的
                    drillSake: dataSource.drillSake                 || params.drillSake,
                    // 事件周边环境
                    eventEnvironment: dataSource.eventEnvironment   || params.eventEnvironment,
                    // 事件等级
                    eventGrade: level?.name                         || params.eventGrade,
                    // 事件等级编码
                    eventGradeCode: level?.value                    || params.eventGradeCode,
                    // 事件性质
                    eventQuality: quality?.name                     || params.eventQuality,
                    // 事件性质编码
                    eventQualityCode: quality?.value                || params.eventQualityCode,
                    // 事件影响范围
                    eventRange: dataSource.eventRange               || params.eventRange,
                    // 事件类型
                    eventType: type?.name                           || params.eventType,
                    // 事件类型编码
                    eventTypeCode: type?.value                      || params.eventTypeCode,
                    // 点位id
                    // pointId: ''                                     || params.pointId, // dataSource.eventPoint,
                    // 点位名称
                    // pointName: ''                                   || params.pointName,
                    // 事件来源
                    // source: sourceSystem.find(v => v.value === dataSource.source)?.name,
                    // 演练开始时间
                    startTime: startTime                            || params.startTime,
                    // 演练结束时间
                    endTime: endTime                                || params.endTime,

                    xcoordinate: dataSource.xCoordinate,
                    ycoordinate: dataSource.yCoordinate,
                    zcoordinate: dataSource.zCoordinate,

                    eventDetails: params.eventDetails
                };

                if ($list) {
                    const Modal = $list.getModal();
                    if (Modal.submitData?.length) {
                        const drillPersonInfoDictTreeNodes = Modal.filterDataSource(Modal.submitData);
                        if (drillPersonInfoDictTreeNodes?.length) {
                            // 演练组织典信息
                            body.drillPersonInfoDictTreeNodes = drillPersonInfoDictTreeNodes;
                        }
                    }
                }

                console.log('onModalSubmitClick', form, dataSource);
                if (dataSource.thumbnailPath || dataSource.jpbPath) {
                    const eventDetails = {};
                    if (dataSource.thumbnailPath) eventDetails.thumbnailPath = dataSource.thumbnailPath;
                    if (dataSource.jpbPath) eventDetails.jpbPath = dataSource.jpbPath;
                    body.eventDetails = JSON.stringify(eventDetails);
                }

                if (body.id) {
                    updateDrill(body)
                }
            }

        } catch (e) {
            console.error(e)
        }
    });
}